package com.hxkj.task.service.impl;

import com.hxkj.MQ.Sender.GeneratorStaticFileService;
import com.hxkj.domain.po.*;
import com.hxkj.service.*;
import com.hxkj.task.service.ScheduleTaskService;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * created by chenfangzhou on 2018/8/17
 */
@Service
@Slf4j
public class ScheduleTaskServiceImpl implements ScheduleTaskService {

    @Autowired
    private ChannelService channelService;
    @Autowired
    private GeneratorStaticFileService generatorStaticFileService;
    @Autowired
    private LeafletService leafletService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ColumnService columnService;
    @Autowired
    private TagsService tagsService;

    //定时任务生成频道静态页
    @Override
    public ResultMessageStruct genChannelPage(String params) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        List<Channel> list = new ArrayList<>();
        try {
            Long siteId = Long.valueOf(params.split("#")[0]);
            if (StringUtils.isNotBlank(params) && params.split("#").length == 2) {
                String channelIds = params.split("#")[1];
                for (String channelId : channelIds.split(",")) {
                    Channel channel = new Channel();
                    channel.setChannelId(Long.valueOf(channelId));
                    channel.setSiteId(siteId);
                    Channel data = channelService.getChannel(channel).getData();
                    list.add(data);
                }
            } else {//没有传频道的id字符串,取所有的频道
                Channel channel = new Channel();
                channel.setSiteId(siteId);
                list = channelService.getAllChannel(channel).getData();
            }
            //发送消息至MQ
            generatorStaticFileService.GenChannelFile(list);
            result.setCode(200);
            result.setMessage("success");
            log.info("所有频道信息发送至MQ~~~~~~~" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


    //定时任务生成单页静态页
    @Override
    public ResultMessageStruct genLeafletPage(String params) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            Long siteId = Long.valueOf(params.split("#")[0]);
            Leaflet leaflet = new Leaflet();
            leaflet.setSiteId(siteId);
            if (StringUtils.isNotBlank(params) && params.split("#").length == 2) {
                String leafletIds = params.split("#")[1];
                leaflet.setLeafletIds(leafletIds);
            }
            //发送消息至MQ
            generatorStaticFileService.GenLeafletFile(leaflet);
            result.setCode(200);
            result.setMessage("success");
            log.info("所有单页信息发送至MQ~~~~~~~" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //定时任务生成文章静态页
    @Override
    public ResultMessageStruct genArticlePage(String params) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            Long siteId = Long.valueOf(params.split("#")[0]);
            Article article = new Article();
            article.setSiteId(siteId);
            if (StringUtils.isNotBlank(params) && params.split("#").length == 2) {
                String articleIds = params.split("#")[1];
                article.setArticleIds(articleIds);
            }
            //发送消息至MQ
            generatorStaticFileService.generateArticleHTML(article);
            result.setCode(200);
            result.setMessage("success");
            log.info("所有文章信息发送至MQ~~~~~~~" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


    //定时任务生成分类静态页
    @Override
    public ResultMessageStruct genColumnPage(String params) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        List<Column> list = new ArrayList<>();
        try {
            Long siteId = Long.valueOf(params.split("#")[0]);
            if (StringUtils.isNotBlank(params) && params.split("#").length == 2) {
                String columnIds = params.split("#")[1];
                Column column = new Column();
                column.setSiteId(siteId);
                column.setColumnIds(columnIds);
                column.setDelTag((byte) 0);
                column.setColumnStatus(0L);//0是启用状态
                list = columnService.getColumnListByTerms(column);
            } else {//没有传单页的id字符串,取所有的频道
                Column column = new Column();
                column.setSiteId(siteId);
                column.setDelTag((byte) 0);
                column.setColumnStatus(0L);//0是启用状态
                list = columnService.getColumnList(column);
            }
            //发送消息至MQ
            generatorStaticFileService.GenColumnFile(list);
            result.setCode(200);
            result.setMessage("success");
            log.info("所有分类信息发送至MQ~~~~~~~" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //定时任务生成Tags详情静态页
    @Override
    public ResultMessageStruct genTagsPage(String params) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        List<Tags> list = new ArrayList<>();
        try {
            Long siteId = Long.valueOf(params.split("#")[0]);
            if (StringUtils.isNotBlank(params) && params.split("#").length == 2) {
                String tagsIds = params.split("#")[1];
                Tags tags = new Tags();
                tags.setSiteId(siteId);
                tags.setTagsIds(tagsIds);
                tags.setDelTag((byte) 0);
                list = tagsService.getShowTagsList(tags);
            } else {//没有传单页的id字符串,取所有的频道
                Tags tags = new Tags();
                tags.setSiteId(siteId);
                tags.setDelTag((byte) 0);
                list = tagsService.getShowTagsList(tags);
            }
            //发送消息至MQ
            generatorStaticFileService.GenTagInfoFile(list);
            result.setCode(200);
            result.setMessage("success");
            log.info("所有Tgas信息发送至MQ~~~~~~~" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //生成整站静态页面
    @Override
    public ResultMessageStruct genTotalSitePage(String params) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            //生成所有频道
            genChannelPage(params);
            //生成所有单页
            genLeafletPage(params);
            //生成所有文章
            genArticlePage(params);
            //生成所有分类
            genColumnPage(params);
            result.setCode(200);
            result.setMessage("success");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
